package com.beiming.kotlindemo.activity

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import com.beiming.kotlindemo.R
import kotlinx.android.synthetic.main.activity_first.*

class FirstActivity : BaseActivity() {

    override fun getContentResId() = R.layout.activity_first

//    override fun resumeSavedInstanceState(savedInstanceState: Bundle?) {
//        super.resumeSavedInstanceState(savedInstanceState)
//    }

    override fun initView() {
        btn1.setOnClickListener {
            Toast.makeText(this, "You clicked Button 1", Toast.LENGTH_SHORT).show()
            Log.d(TAG, "CLICK")
//            finish()
        }
        btn_to_secondactivity.setOnClickListener {
            //显示Intent
//            val intent = Intent(this, SecondActivity::class.java)
//            startActivity(intent)

            //隐示Intent
            val intent1 = Intent("com.beiming.kotlindemo.ACTION_START")
            intent1.addCategory("com.beiming.kotlindemo.MY_CATEGORY")
            val data = "Hello SecondActivity"
            intent1.putExtra("extra_data", data)
            startActivityForResult(intent1, 1)

            //静态方法调用
            SecondActivity.actionStart(
                this,
                "data1",
                "data2"
            )
        }
        btn_open_internet_explorer.setOnClickListener {
            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("https://www.baidu.com")
            startActivity(intent)
        }
        btn_to_listviewactivity.setOnClickListener {
            startActivity(Intent(this, ListviewActivity::class.java))
        }
        btn_to_recyclerviewactivity.setOnClickListener {
            startActivity(Intent(this, RecyclerviewActivity::class.java))
        }
    }

    override fun initData() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1 && resultCode == 2) {
            val extraData = data?.getStringExtra("data_return");
            Log.d(TAG, "returned data is $extraData")
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item?.itemId) {
            R.id.add_item -> Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show()
            R.id.remove_item -> Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT)
                .show()
        }
        return true
    }

    //保存数据
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
    }
}
